import "antd/lib/message/style/css";
import _message from "antd/lib/message";
import "antd/lib/modal/style/css";
import _Modal from "antd/lib/modal";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import { FileTextOutlined } from '@ant-design/icons';
import React from 'react';
import qs from 'qs';
import localStorage from '../utils/localStorage';
import PreView from './preView';
import { getConfigure } from '../utils/configure'; // 获取默认配置

export var createUploadUrl = function createUploadUrl() {
  var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  return "".concat(getConfigure().uploadApi).concat(qs.stringify(_objectSpread(_objectSpread({}, params), {}, {
    Client: getConfigure().client,
    Authorization: localStorage.getItem('token')
  }), {
    addQueryPrefix: true,
    allowDots: true // 多层级对象

  }));
}; // 预览文件功能

export var preViewFile = function preViewFile(e) {
  var fileData = {
    fileId: e.fileId,
    fileCode: e.fileCode,
    fileName: e.fileName,
    Client: getConfigure().client,
    Authorization: localStorage.getItem('token')
  };

  if (!fileData.fileName && e.name) {
    fileData.fileName = e.name;
  }

  if (!fileData.fileId && e.response && e.response.models) {
    fileData.fileId = e.response.models[0] && e.response.models[0].id;
  }

  if (!fileData.fileCode && e.response && e.response.models) {
    fileData.fileId = e.response.models[0] && e.response.models[0].code;
  }

  var downLoadURL = "".concat(getConfigure().downloadApi).concat(qs.stringify({
    fileId: fileData.fileId,
    fileCode: fileData.fileCode,
    Client: fileData.Client,
    Authorization: fileData.Authorization
  }, {
    addQueryPrefix: true,
    allowDots: true
  }));

  _Modal.info({
    icon: /*#__PURE__*/React.createElement(FileTextOutlined, null),
    title: fileData.fileName,
    content: /*#__PURE__*/React.createElement("p", {
      style: {
        paddingTop: '10px'
      }
    }, /*#__PURE__*/React.createElement(PreView, {
      viewUrl: downLoadURL,
      viewId: fileData.fileId,
      viewCode: fileData.fileCode,
      viewName: fileData.fileName,
      style: {
        marginRight: '10px'
      }
    }), /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("a", {
      href: downLoadURL,
      onClick: function onClick() {
        _Modal.destroyAll();
      },
      download: fileData.fileName,
      target: "_blank",
      rel: "noopener noreferrer"
    }, "\u4E0B\u8F7D"))),
    okText: '关闭',
    onOk: function onOk() {
      _Modal.destroyAll();
    }
  });

  return true;
};
/*
 * 上传文件之前的钩子，参数为上传的文件，若返回 false 则停止上传
 * 上传前过滤一些危险文件
 * */

export var onBeforeUpload = function onBeforeUpload(file, fileList) {
  var mess = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  var arr = ['cmd', 'exe', 'bat', 'vbs', 'dll'];
  var as = file.name && file.name.split('.')[1];

  if (arr.indexOf(as) >= 0) {
    if (!mess) {
      _message.error('该文件存在风险，不可上传!');
    }

    return false;
  }

  var isLt20M = file.size / 1024 / 1024 <= 50;

  if (file.size && !isLt20M) {
    if (!mess) {
      _message.error('请选择小于50MB的文件!');
    }

    return false;
  }

  return true;
}; // 生成带参数链接

export var createParamsUrl = function createParamsUrl(params, target, url) {
  var baseApi = '';

  if (target === 'download') {
    // 下载
    baseApi = getConfigure().downloadApi;
  } else if (target === 'upload') {
    // 上传
    baseApi = getConfigure().uploadApi;
  } else {
    // 自定义
    baseApi = url;
  }

  return "".concat(baseApi).concat(qs.stringify(_objectSpread(_objectSpread({}, params), {}, {
    Client: getConfigure().client,
    Authorization: localStorage.getItem('token')
  }), {
    addQueryPrefix: true,
    allowDots: true // 多层级对象

  }));
};